﻿using Halcon二次开发.Controller.缺陷检测;
using Halcon二次开发.Controller.数据加载与保存;
using Halcon二次开发.Model.自定义控件;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.View.多模板匹配;
using Halcon二次开发.View.相机;

namespace Halcon二次开发.View.缺陷检测
{
    public partial class BoldForm : Form
    {
        HwindowFlowNodeUrseControl hwindowFlowNode;
        public BoldController controller;

        string nodeName;
        public BoldForm(string name, HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            InitializeComponent();
            this.hwindowFlowNode = hwindowFlowNode;
            this.nodeName = name;
            this.controller = new BoldController($"Data\\工程文件\\{hwindowFlowNode.name}工程文件\\精准测量工具卡尺校验数据\\" + nodeName, hwindowFlowNode,this);
            throldComboBox.SelectedIndex = 1;
            ScaleComboBox.SelectedIndex = 1;
            selectComboBox.SelectedIndex = 1;
            comboBox1.SelectedIndex = 1;
        }

        public void SetJsonPaths(string path, string addres)
        {
            string paths = path + "\\" + "界面数据\\" + nodeName + "\\" + addres + "数据.json";
            //调用存储方法
            new LoadData().LoadDatas(paths, this);
        }

        /// <summary>
        /// 画矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                //点击绘制时获取模板匹配数据
                foreach (TreeNode item in hwindowFlowNode.treeView1.Nodes)
                {
                    if (item.Name.Contains("模型匹配"))
                    {
                        controller.HideIntputArrayRows = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayRows;
                        controller.HideIntputArrayColumns = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayColumns;
                        controller.HideIntputArrayPhis = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayPhis;
                    }
                    else if (item.Name.Contains("相机"))
                    {
                        controller.HideIntputImage = (item.Tag as CamerForm).controller.HideOutputImage;
                    }
                }

                if (controller.HideIntputImage == null)
                {
                    (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + "当前图像为空!\r\n");
                    return;
                }
                controller.DrawRecangle();
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
            
        }

        /// <summary>
        /// 反射调用该方法更新窗体句柄
        /// </summary>
        /// <param name="hwindowFlowNode"></param>
        public void PushHWindon(HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            this.hwindowFlowNode = hwindowFlowNode;
            controller.hwindowFlowNode = hwindowFlowNode;
        }
        /// <summary>
        /// 放置卡尺
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            try
            {
                controller.Put_Recangle2();
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                controller.Tool_Run();
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }

        /// <summary>
        /// 动态调整瑕疵面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void throldMax_ValueChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    //点击绘制时获取模板匹配数据
            //    foreach (TreeNode item in hwindowFlowNode.treeView1.Nodes)
            //    {
            //        if (item.Name.Contains("模型匹配"))
            //        {
            //            controller.HideIntputArrayRows = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayRows;
            //            controller.HideIntputArrayColumns = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayColumns;
            //            controller.HideIntputArrayPhis = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayPhis;
            //        }
            //        else if (item.Name.Contains("相机"))
            //        {
            //            controller.HideIntputImage = (item.Tag as CamerForm).controller.HideOutputImage;
            //        }
            //    }

            //    if (controller.HideIntputImage == null)
            //    {
            //        (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + "当前图像为空!\r\n");
            //        return;
            //    }
            //    if (controller.HideIntputArrayRows!=null) {
            //        controller.Updata_Bold();
            //    }
                
            //}
            //catch (Exception ex)
            //{
            //    (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            //}
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                
            }
            else {
                throldComboBox.SelectedIndex = 1;
                ScaleComboBox.SelectedIndex = 1;
                selectComboBox.SelectedIndex = 1;
            }
        }

        private void throldComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (throldComboBox.SelectedIndex == 1)
            {
                    throldMin.Enabled = false;
                    throldMax.Enabled = false;
            }
            else
            {
                throldMin.Enabled = true;
                throldMax.Enabled = true;
            }
        }

        private void ScaleComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (throldComboBox.SelectedIndex == 1)
            {
                scaleMax.Enabled = false;
                scaleMin.Enabled = false;
            }
            else
            {
                scaleMax.Enabled = true;
                scaleMin.Enabled = true;
            }
        }

        private void selectComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (selectComboBox.SelectedIndex == 1)
            {
                MaxArea.Enabled = false;
                MinArea.Enabled = false;
            }
            else
            {
                MaxArea.Enabled = true;
                MinArea.Enabled = true;
            }
        }
    }
}
